package com.sanyue.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 复杂参数
 * @author: Guan
 **/

@Controller
public class RequestController {

    @GetMapping("/register")
    public String register(Map<String,Object>map,
                           Model model,
                           HttpServletResponse response){
        //模拟前端传来的数据
        //map中的数据和model中的数据会被放入到request域中。底层：request.setAttribute()
        //注意：是将数据放入的请求域中，不是将map对象和model对象放入请求域中
        map.put("user","gpy");
        map.put("password","12345");
        model.addAttribute("salary",800000);

        //使用HttpServletResponse
        //创建一个cookie，并通过response传入到浏览器中
        //可以在浏览器-检查-application-cookie中查看传入的cookie信息
        Cookie cookie = new Cookie("email", "gpyue");
        response.addCookie(cookie);

        //转发
        return "forward:/ok";

    }

    @ResponseBody
    @GetMapping("/ok")
    public String register_Ok(@RequestAttribute(value = "user")String user,
                              HttpServletRequest request,
                              @RequestAttribute("salary") String salary){
        //测试request域相关使用
        System.out.println("user = " + user);
        System.out.println("------------------------");
        Object password = request.getAttribute("password");
        System.out.println("password = " + password);
        System.out.println("------------------------");
        System.out.println("salary = " + salary);
        return "success";
    }
}
